package com.notemanagement.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.notemanagement.R;
import com.notemanagement.adapter.OrderAdapter;
import com.notemanagement.app.AppContext;
import com.notemanagement.bean.OrderDetailInfo;
import com.notemanagement.database.SQLiteHelper;
import com.notemanagement.utils.Constant;

import java.util.List;

/**
 * Created by 金 on 2015/1/17.
 */
public class Order extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener{

    private ListView orderlist;
    private List<OrderDetailInfo> infos;
    private OrderAdapter orderAdapter;
    private ImageView back_image, add_order_image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order);
        back_image = (ImageView) findViewById(R.id.order_head_back);
        add_order_image = (ImageView) findViewById(R.id.order_add_new);

        orderlist = (ListView) findViewById(R.id.order_list);
        init();
        orderAdapter = new OrderAdapter(infos, this);
        orderlist.setAdapter(orderAdapter);
    }

    private void init(){
        infos = Constant.db.queryOrderList();
        back_image.setOnClickListener(this);
        add_order_image.setOnClickListener(this);
        orderlist.setOnItemClickListener(this);
        orderlist.setOnItemLongClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.order_add_new:
                Intent intent = new Intent(this, AddOrder.class);
                startActivityForResult(intent, 0);
                break;
            case R.id.order_head_back:
                this.finish();
                break;
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, OrderDetail.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("detail", infos.get(position));
        intent.putExtra("bundle", bundle);
        startActivity(intent);

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            final  int index = position;
            AlertDialog.Builder builder = new AlertDialog.Builder(this)
                    .setTitle("提示")
                     .setMessage("确定要删除?")
                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            long ID = infos.get(index).id;
                            Log.d("king", "id = " + ID);
                            if (ID >= 0) {
                                Constant.db.deleteItem(ID);
                                infos.remove(index);
                                orderAdapter.notifyDataSetChanged();
                            }

                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
            builder.create().show();
        return  true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 0:
                if(resultCode == 0){
                    Log.d("king", "resule");
                    Bundle bundle = data.getBundleExtra("bundle");
                    infos.add((OrderDetailInfo)bundle.getSerializable("detail"));
                     Log.d("king", "size = " + infos.size());
                    orderAdapter.notifyDataSetChanged();
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
